﻿using CDSysLDManageProject.BYDTableAdapters;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;

namespace CDSysLDManageProject
{
    /// <summary>
    /// 甲进充放电设备通讯协议指令集
    /// </summary>
    public class 
        KapjinEqProtocol
    {
       
        MySocket iSocket;
        string PCID = "D101";//充放电设备PC ID
        int OutTimeSet = 10;
        int chcount = 16;//单位托盘电芯个数

        byte[] stx = { 0x02 };//STX
        byte[] etx = { 0x03 };//ETX

        BYDMesDAL mdal = new BYDMesDAL();

        public KapjinEqProtocol(MySocket mySocket)
        {
            iSocket = mySocket;
            GParam.RecListDic.Add("ETI", new List<string>());
            GParam.RecListDic.Add("EPF", new List<string>());
            GParam.RecListDic.Add("ERS", new List<string>());
            GParam.RecListDic.Add("ESC", new List<string>());
            //GParam.RecListDic.Add("ERD", new List<string>());
            GParam.RecListDic.Add("EDD", new List<string>());
            GParam.RecListDic.Add("EUR", new List<string>());
            GParam.RecListDic.Add("ETL", new List<string>());
        }

        /// <summary>
        /// 心跳
        /// </summary>
        /// <returns></returns>
        public bool AliveSignal()
        {
            if (!GParam.RecStringDic.ContainsKey("EAL"))
            {
                GParam.RecStringDic.Add("EAL", "");
            }
            else
            {
                GParam.RecStringDic["EAL"] = "";
            }

            string sendData = Encoding.ASCII.GetString(stx) + "WAL" + PCID + "000000000" + Encoding.ASCII.GetString(etx);
            iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
            //Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
            Log.SendToECS.Add(sendData);
            int NoReplyCount = 0;
            while (NoReplyCount < OutTimeSet)
            {
                string replymessage = GParam.RecStringDic["EAL"];
                if (!string.IsNullOrEmpty(replymessage))
                {
                    try
                    {
                        if (replymessage.Substring(4, 4) == PCID)
                        {
                            return true;
                        }
                    }
                    catch
                    { 
                    
                    }
                    
                }

                NoReplyCount++;
                Thread.Sleep(500);
                continue;

            }
            return false;
        }

        /// <summary>
        /// 时间同步
        /// </summary>
        /// <returns></returns>
        public bool SynchronizeTime()
        {
            if (!GParam.RecStringDic.ContainsKey("EST"))
            {
                GParam.RecStringDic.Add("EST", "");
            }
            else
            {
                GParam.RecStringDic["EST"] = "";
            }

            string sendData = Encoding.ASCII.GetString(stx) + "WST" + PCID + "000000000"
                + DateTime.Now.ToString("yyyyMMddHHmmss") + Encoding.ASCII.GetString(etx);
            iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
            //Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
            Log.SendToECS.Add(sendData);
            int NoReplyCount = 0;
            while (NoReplyCount < OutTimeSet)
            {
                string replymessage = GParam.RecStringDic["EST"];
                if (!string.IsNullOrEmpty(replymessage))
                {
                    try
                    {
                        string iPCID = replymessage.Substring(4, 4);
                        string replyResult = replymessage.Substring(17, 1);
                        if (iPCID == PCID)
                        {
                            if (replyResult == "1")
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                    catch
                    {

                    }

                }

                NoReplyCount++;
                Thread.Sleep(500);
                continue;

            }
            throw new Exception("与充放电设备同步时间时发生异常，可能为通讯超时！发送内容：" + sendData);
        }

        /// <summary>
        /// 托盘信息请求响应
        /// </summary>
        /// <returns></returns>
        public bool SendTrayInfo(string TrayID,DataTable dtcellinfo)
        {

                            int ngcount = 0;
           
                            string StageLoc = "001001001";
                            
                            
                            string RecipeID = mdal.GetRecipeByTrayType(TrayID);
                            //string RecipeID = "001";
                            //根据扫描的dt获取电池条码
                            string cellno = mdal.GetCellInfoByTrayIdTest(dtcellinfo);
                            string sendData = "WTI" + PCID;
                            sendData = sendData + StageLoc + TrayID.PadRight(20, ' ') + RecipeID.PadRight(10, ' ');//左补NULL ---甲进是右补" "
                            string cellsts = mdal.GetCellStsTest1(dtcellinfo);
                            sendData = sendData + cellsts;
                            sendData = sendData + cellno;
                            
                            //sendData += "1";
                            //Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
                            Log.SendToECS.Add(sendData);
                            sendData = Encoding.ASCII.GetString(stx) + sendData + Encoding.ASCII.GetString(etx);
                            bool flag=iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));

                            while (flag != true && ngcount <= 3)
                            {
                                flag = iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
                                //ngcount++;
                            }
                            if (flag == false)
                            {

                                //Log.WriteLog(LogFile.SystemError, "通知ECS EPF失败，可能是网络连接导致的");
                                Log.SystemError.Add("通知ECS WTI失败，可能是网络连接导致的");
                            }

                            return flag;
        }


        /// <summary>1
        /// 同步架台信息
        /// </summary>
        /// <returns>架台状态列表（无架台位置编号，有顺序号）</returns>
        public bool  SynStageStatus()
        {
            List<StageStatusMod> stageStList = new List<StageStatusMod>();
            if (!GParam.RecStringDic.ContainsKey("ESS"))
            {
                GParam.RecStringDic.Add("ESS", "");
            }
            else
            {
                GParam.RecStringDic["ESS"] = "";
            }

            string sendData = Encoding.ASCII.GetString(stx) + "WSS" + PCID + "000000000" + Encoding.ASCII.GetString(etx);
            iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
            //Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
            Log.SendToECS.Add(sendData);
            int NoReplyCount = 0;
           
            while (NoReplyCount < OutTimeSet)
            {
                string replymessage = GParam.RecStringDic["ESS"];
                if (!string.IsNullOrEmpty(replymessage))
                {
                    try
                    {
                        if (replymessage.Substring(4, 4) == PCID)
                        {
                            
                            int stagecount = Convert.ToInt32(replymessage.Substring(17, 3));
                            for (int i = 0; i < stagecount; i++)
                            {
                                StageStatusMod stageStatus = new StageStatusMod();
                                stageStatus.Stageindex = i;
                                
                                stageStatus.Workmode = replymessage.Substring(20 + i * 4,1);
                                stageStatus.Currentst = replymessage.Substring(20 + i * 4 + 1, 1);
                                stageStatus.Chargest = replymessage.Substring(20 + i * 4 + 2, 2);
                                if (stageStatus.Workmode == "1" && stageStatus.Currentst == "1" && stageStatus.Chargest == "08")
                                {
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                                //stageStatus.Channelst = replymessage.Substring(20 + 4 * stagecount + i * 5, 1);
                                //stageStatus.Emgst = replymessage.Substring(20 + 4 * stagecount + i * 5 + 1, 4);
                                //stageStList.Add(stageStatus);
                            }
                           
                        }
                    }
                    catch
                    {

                    }

                }

                NoReplyCount++;
                Thread.Sleep(500);
                continue;

            }
            return false;
            //throw new Exception("与充放电设备同步架台信息时发生异常！");
        }


        ///// <summary>
        ///// 将架台信息存入数据库
        ///// </summary>
        ///// <returns>架台状态列表（无架台位置编号，有顺序号）</returns>
        //public void  SaveStageStatus(List<StageStatusMod> lssm)
        //{
        //   tbl_stageinfoTableAdapter stageinfo = new tbl_stageinfoTableAdapter();
        //   //stageinfo.GetStageInfoNo();
        //   //DataTable dt = new DataTable();
        //   //dt = stageinfo.GetCurrentTask();
        //   string StageInfoNo = stageinfo.GetStageInfoNo().ToString();
           
        //    //int  StageInfoNo = int.Parse(stageinfo.GetStageInfoNo().ToString());
        //   if (int.Parse(StageInfoNo) > 0)
        //    {
        //        for (int i = 0; i < lssm.Count; i++)
        //        {
        //            //int stageindex = lssm[i].Stageindex;
        //            string Stagelocation = lssm[i].Stagelocation.ToString();
        //            string Workmode = lssm[i].Workmode.ToString();
        //            string Currentst = lssm[i].Currentst.ToString();
        //            string Chargest = lssm[i].Chargest.ToString();
        //            string Channelst = lssm[i].Channelst.ToString();
        //            string Emgst = lssm[i].Emgst.ToString();

        //            stageinfo.UpdateStageStatus(Workmode,Currentst,Chargest,Channelst,Emgst,Stagelocation);

        //        }
        //    }
        //    else
        //    {//不能存在架台信息就插入存在的话就更新
        //        for (int i = 0; i < lssm.Count; i++)
        //        {
        //            int stageindex = lssm[i].Stageindex;
        //            string Stagelocation = lssm[i].Stagelocation.ToString();
        //            string Workmode = lssm[i].Workmode.ToString();
        //            string Currentst = lssm[i].Currentst.ToString();
        //            string Chargest = lssm[i].Chargest.ToString();
        //            string Channelst = lssm[i].Channelst.ToString();
        //            string Emgst = lssm[i].Emgst.ToString();
        //            string receipeid = "";
        //            if (lssm[i].Stagelocation.ToString().Substring(5, 1) == "1" || lssm[i].Stagelocation.ToString().Substring(5, 1) == "2")
        //            {
        //                receipeid = "C19";
        //            }else
        //            {
        //                receipeid = "C15";
        //            }
        //            stageinfo.InsertStageInfo(stageindex, Stagelocation, Workmode, Currentst, Chargest, Channelst, Emgst, 0, null, null, receipeid);

        //        }
        //    }
            
        //}


/// <summary>
/// 托盘装入架台
/// </summary>
/// <param name="TrayNo"></param>
/// <param name="StageLoc"></param>
/// <returns></returns>
        public bool TrayLoad(string TrayNo,string StageLoc)
        {
            int ngcount = 1;
            if (!GParam.RecListDic.ContainsKey("ETL"))
            {
                GParam.RecListDic.Add("ETL", new List<string>());
            }
            TrayNo = TrayNo.PadRight(20,' ');
            string sendData = Encoding.ASCII.GetString(stx) + "WTL" + PCID + StageLoc
                + TrayNo + Encoding.ASCII.GetString(etx);
            bool flag=iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));

            Thread.Sleep(2500);
            while (flag != true && ngcount <= 3)
            {
                flag = iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
                ngcount++;
            }
            if (flag == false)
            {

                //Log.WriteLog(LogFile.SystemError, "通知ECS ETL失败，可能是网络连接导致的");
                Log.SystemError.Add("通知ECS ETL失败，可能是网络连接导致的");
            }

            Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
            int NoReplyCount = 0;
            bool replyresult = false;
            while (NoReplyCount < OutTimeSet)
            {
                List<string> replyList = GParam.RecListDic["ETL"];
                if (replyList.Count>0)
                {
                    try
                    {
                        bool rectag = false;
                        for (int i = 0; i < replyList.Count; i++)
                        {
                            try
                            {
                                string replymessage = replyList[i];
                                string iPCID = replymessage.Substring(4, 4);
                                string replyLoc = replymessage.Substring(8, 9);
                                string replyResult = replymessage.Substring(17, 1);
                                if (iPCID == PCID && replyLoc == StageLoc)
                                {
                                    rectag = true;//确认收到响应
                                    if (replyResult == "1")
                                    {
                                        replyresult = true;
                                    }
                                    else
                                    {
                                        replyresult = false;
                                    }
                                    replyList.RemoveAt(i);
                                    i--;
                                }
                            }
                            catch
                            {
                                replyList.RemoveAt(i);
                                i--;
                            }
                        }
                        if (rectag)//确认收到响应再返回结果
                        {
                            return replyresult;
                        }
                    }
                    catch(Exception ex)
                    {
                        Exceptionlog.InsertException(ex);
                    }

                }

                NoReplyCount++;
                Thread.Sleep(500);
                continue;

            }
            return false;
            //throw new Exception("与充放电设备执行托盘到位通讯时发生异常，可能为通讯超时");
        }


        /// <summary>
        /// 充放电工艺完成
        /// </summary>
        /// <returns>完成货架位列</returns>
        public bool ProcessFinish()
        {
            int ngcount = 0;
            if (!GParam.RecListDic.ContainsKey("EPF"))
            {
                GParam.RecListDic.Add("EPF", new List<string>());
                return false;
            }
            List<string> reciveList = GParam.RecListDic["EPF"];
            GParam.RecStringDic["EPF"] = "";
            //List<string> locList = new List<string>();
            while (reciveList.Count > 0)
            {
                if (reciveList[0].Length > 16)
                {
                    string recMessage = reciveList[0];
                    if (recMessage.Substring(4, 4) == PCID)
                    {
                        string StageLoc = recMessage.Substring(8, 9);
                        string replyresult = "1";
                        //locList.Add(StageLoc);
                        string sendData = Encoding.ASCII.GetString(stx) + "WPF" + PCID + StageLoc
                + replyresult + Encoding.ASCII.GetString(etx);
                        bool flag=iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));

                        while (flag != true && ngcount <= 3)
                        {
                            flag = iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
                            ngcount++;
                        }
                        if (flag == false)
                        {

                            //Log.WriteLog(LogFile.SystemError, "通知"+StageLoc+ "EPF失败，可能是网络连接导致的");
                            Log.SystemError.Add("通知" + StageLoc + "EPF失败，可能是网络连接导致的");
                        }

                        //Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
                        Log.SendToECS.Add(sendData);
                    }

                }
                reciveList.RemoveAt(0);
                
                
            }
            return true;
        }

        /// <summary>
        /// 托盘取出架台
        /// </summary>
        /// <param name="TrayNo">托盘号</param>
        /// <param name="StageLoc">架台编号</param>
        /// <returns></returns>
        public bool TrayUnload(string TrayNo, string StageLoc)
        {
            int ngcount = 1;
            if (!GParam.RecListDic.ContainsKey("ETU"))
            {
                GParam.RecListDic.Add("ETU", new List<string>());
            }

            string sendData = Encoding.ASCII.GetString(stx) + "WTU" + PCID + StageLoc
                + TrayNo + Encoding.ASCII.GetString(etx);
            bool flag = iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));

            while (flag != true && ngcount <= 3)
            {
                flag = iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
                ngcount++;
            }
            if (flag == false)
            {

                //Log.WriteLog(LogFile.SystemError, "通知ECS ETU失败，可能是网络连接导致的");
                Log.SystemError.Add("通知ECS ETU失败，可能是网络连接导致的");
            }
            Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
            int NoReplyCount = 0;
            bool replyresult = false;
            while (NoReplyCount < OutTimeSet)
            {
                List<string> replyList = GParam.RecListDic["ETU"];
                if (replyList.Count > 0)
                {
                    try
                    {
                        bool rectag = false;
                        for (int i = 0; i < replyList.Count; i++)
                        {
                            try
                            {
                                string replymessage = replyList[i];
                                string iPCID = replymessage.Substring(4, 4);
                                string replyLoc = replymessage.Substring(8, 9);
                                string replyResult = replymessage.Substring(17, 1);
                                if (iPCID == PCID && replyLoc == StageLoc)
                                {
                                    rectag = true;//确认收到响应
                                    if (replyResult == "1")
                                    {
                                        replyresult = true;
                                    }
                                    else
                                    {
                                        replyresult = false;
                                    }
                                    replyList.RemoveAt(i);
                                    i--;
                                }
                            }
                            catch(Exception ex)
                            {
                                Exceptionlog.InsertException(ex);
                                replyList.RemoveAt(i);
                                i--;
                            }
                        }
                        if (rectag)//确认收到响应再返回结果
                        {
                            return replyresult;
                        }
                    }
                    catch (Exception ex)
                    {
                        Exceptionlog.InsertException(ex);
                    }

                }

                NoReplyCount++;
                Thread.Sleep(500);
                continue;

            }
            return false;
        }

        /// <summary>
        /// 预约架台位置
        /// </summary>
        /// <param name="StageLoc"></param>
        /// <returns></returns>
        public bool StageReservation(string StageLoc)
        {
            if (!GParam.RecListDic.ContainsKey("ESR"))
            {
                GParam.RecListDic.Add("ESR", new List<string>());
            }

            string sendData = Encoding.ASCII.GetString(stx) + "WSR" + PCID + StageLoc
                 + Encoding.ASCII.GetString(etx);
            iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
            Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
            int NoReplyCount = 0;
            bool replyresult = false;
            while (NoReplyCount < OutTimeSet)
            {
                List<string> replyList = GParam.RecListDic["ESR"];
                if (replyList.Count > 0)
                {
                    try
                    {
                        bool rectag = false;
                        for (int i = 0; i < replyList.Count; i++)
                        {
                            try
                            {
                                string replymessage = replyList[i];
                                string iPCID = replymessage.Substring(4, 4);
                                string replyLoc = replymessage.Substring(8, 9);
                                string replyResult = replymessage.Substring(17, 1);

                                if (iPCID == PCID && replyLoc == StageLoc)
                                {
                                    rectag = true;//确认收到响应
                                    if (replyResult == "1")
                                    {
                                        replyresult = true;
                                    }
                                    else
                                    {
                                        replyresult = false;
                                    }
                                    replyList.RemoveAt(i);
                                    i--;
                                }
                            }
                            catch(Exception ex)
                            {
                                Exceptionlog.InsertException(ex);
                                replyList.RemoveAt(i);
                                i--;
                            }
                        }
                        if (rectag)//确认收到响应再返回结果
                        {
                            return replyresult;
                        }
                    }
                    catch (Exception ex)
                    {
                        Exceptionlog.InsertException(ex);
                        break;
                        
                    }

                }

                NoReplyCount++;
                Thread.Sleep(1000);
                continue;

            }
            return false;
           // throw new Exception("与充放电设备执行预约架台位置通讯时发生异常！");
        }




        /// <summary>
        /// 卸载托盘预约(销钉下降)
        /// </summary>
        /// <param name="StageLoc"></param>
        /// <returns></returns>
        public bool UpLoadingReservation(string StageLoc)
        {
            int ngcount = 1;
            if (!GParam.RecListDic.ContainsKey("EUR"))
            {
                GParam.RecListDic.Add("EUR", new List<string>());
            }

            string sendData = Encoding.ASCII.GetString(stx) + "WUR" + PCID + StageLoc
                 + Encoding.ASCII.GetString(etx);
            bool flag=iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
            while (flag != true && ngcount<=3)
            {
                flag = iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
                ngcount++;
            }
            if (flag == false)
            {

                //Log.WriteLog(LogFile.SystemError,"通知ECS EUR失败，可能是网络连接导致的");
                Log.SystemError.Add("通知ECS EUR失败，可能是网络连接导致的");
            }
           
            Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
            int NoReplyCount = 0;
            bool replyresult = false;
            while (NoReplyCount < OutTimeSet)
            {
                List<string> replyList = GParam.RecListDic["EUR"];
                if (replyList.Count > 0)
                {
                    try
                    {
                        bool rectag = false;
                        for (int i = 0; i < replyList.Count; i++)
                        {
                            try
                            {
                                string replymessage = replyList[i];
                                string iPCID = replymessage.Substring(4, 4);
                                string replyLoc = replymessage.Substring(8, 9);
                                string replyResult = replymessage.Substring(17, 1);

                                if (iPCID == PCID && replyLoc == StageLoc)
                                {
                                    rectag = true;//确认收到响应
                                    if (replyResult == "1")
                                    {
                                        replyresult = true;
                                    }
                                    else
                                    {
                                        replyresult = false;
                                    }
                                    replyList.RemoveAt(i);
                                    i--;
                                }
                            }
                            catch (Exception ex)
                            {
                                Exceptionlog.InsertException(ex);
                                replyList.RemoveAt(i);
                                i--;
                            }
                        }
                        if (rectag)//确认收到响应再返回结果
                        {
                            return replyresult;
                        }
                    }
                    catch (Exception ex)
                    {
                        Exceptionlog.InsertException(ex);
                        break;
                    }

                }

                NoReplyCount++;
                Thread.Sleep(1000);
                continue;

            }
            return false;
            //throw new Exception("与充放电设备执行卸载托盘预约时发生异常！");
        }

        ///// <summary>
        ///// 架台状态更新（单条）
        ///// </summary>
        ///// <returns>成功/失败</returns>
        //public bool StageStatusChanged()
        //{
        //    int ngcount = 0;
        //    if (!GParam.RecListDic.ContainsKey("ERS"))
        //    {
        //        GParam.RecListDic.Add("ERS", new List<string>());
        //        return true;
        //    }
        //    List<string> reciveList = GParam.RecListDic["ERS"];
        //    GParam.RecStringDic["ERS"] = "";
        //    while (reciveList.Count > 0)
        //    {
        //        if (reciveList[0].Length > 26)
        //        {
        //            try
        //            {
        //                string recMessage = reciveList[0];
        //                if (recMessage.Substring(4, 4) == PCID)
        //                {
        //                    StageStatusMod stageStatus = new StageStatusMod();
        //                    stageStatus.Stagelocation = recMessage.Substring(8, 9);
        //                    stageStatus.Workmode = recMessage.Substring(17, 1);
        //                    stageStatus.Currentst = recMessage.Substring(18, 1);
        //                    stageStatus.Chargest = recMessage.Substring(19, 2);
        //                    stageStatus.Channelst = recMessage.Substring(20, 1);
        //                    stageStatus.Emgst = recMessage.Substring(21, 4);
        //                    bool result = mdal.UpdateStageStatus(stageStatus);
        //                    string replyresult = "1";
        //                    if (result)
        //                    {
        //                        Exceptionlog.InsertException("更新架台信息发生异常", "与数据库连接发生异常，数据更新失败!");
        //                        replyresult = "0";
        //                    }

        //                    string sendData = Encoding.ASCII.GetString(stx) + "WRS" + PCID + recMessage.Substring(8, 9)
        //                    + replyresult + Encoding.ASCII.GetString(etx);
        //                    bool flag=iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));

        //                    while (flag != true && ngcount <= 3)
        //                    {
        //                        flag = iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
        //                        ngcount++;
        //                    }
        //                    if (flag == false)
        //                    {

        //                        //Log.WriteLog(LogFile.SystemError, "通知ECS EPF失败，可能是网络连接导致的");
        //                        Log.SystemError.Add("通知ECS EPF失败，可能是网络连接导致的");
        //                    }

        //                    //Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
        //                    Log.SendToECS.Add(sendData);


        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                Exceptionlog.InsertException(ex);
        //            }

        //        }
        //        reciveList.RemoveAt(0);
        //        if (GParam.RecListDic["ERS"].Count > 0)
        //        {
        //            GParam.RecListDic["ERS"].RemoveAt(0);
        //        }
                
        //    }
        //    return true;
        //}

        /// <summary>
        /// ECS请求获取堆垛机叉子位置状态
        /// </summary>
        /// <returns></returns>
        public bool RequestStackerCraneStatus()
        {
            if (!GParam.RecListDic.ContainsKey("ESC"))
            {
                GParam.RecListDic.Add("ESC", new List<string>());
                return false;
            }
            List<string> reciveList = GParam.RecListDic["ESC"];
            List<string> locList = new List<string>();
            while (reciveList.Count > 0)
            {
                if (reciveList[0].Length > 16)
                {
                    string recMessage = reciveList[0];
                    if (recMessage.Substring(4, 4) == PCID)
                    {
                        string StageLoc = recMessage.Substring(8, 9);
                        string replyresult = mdal.GetLDForkStByStageLoc(StageLoc);
                        locList.Add(StageLoc);
                        string sendData = Encoding.ASCII.GetString(stx) + "WSC" + PCID + StageLoc
                + replyresult + Encoding.ASCII.GetString(etx);
                        iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
                        Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
                    }

                }
                reciveList.RemoveAt(0);
                if (GParam.RecListDic["ESC"].Count > 0)
                {
                    GParam.RecListDic["ESC"].RemoveAt(0);
                }
               
            }
            return true;
        }

        /// <summary>
        /// 充放电结果
        /// </summary>
        /// <returns></returns>
        /// 
        //public bool StepResult()
        public bool StepResult()
        {
            int ngcount = 0;
            if (!GParam.RecListDic.ContainsKey("EDD"))
            {
                GParam.RecListDic.Add("EDD", new List<string>());
                return false;
            }
            List<string> reciveList = GParam.RecListDic["EDD"];
            GParam.RecStringDic["EDD"] = "";
            while (reciveList.Count > 0)
            {
                
                //if (reciveList[0].Length >= 171)
                //{
                    string recMessage = reciveList[0];
                    try
                    {
                        DateTime DCIREndTime = DateTime.Now;
                        if (recMessage.Substring(4, 4) == PCID)
                        {
                            int leng = recMessage.Length;
                           
                            CDStepResult cdResult = new CDStepResult();
                            
                            cdResult.TrayID = recMessage.Substring(17, 20);
                            cdResult.StageLoc = recMessage.Substring(8, 9);

                            cdResult.RecipeID = recMessage.Substring(37, 10);
                            string starttime = recMessage.Substring(47, 14);
                            cdResult.StartTime = DateTime.Parse(starttime.Substring(0, 4) + "-" + starttime.Substring(4, 2) + "-" + starttime.Substring(6, 2) + " " + starttime.Substring(8, 2) + ":" + starttime.Substring(10, 2) + ":" + starttime.Substring(12, 2));
                            string endtime = recMessage.Substring(61, 14);
                            cdResult.EndTime = DateTime.Parse(endtime.Substring(0, 4) + "-" + endtime.Substring(4, 2) + "-" + endtime.Substring(6, 2) + " " + endtime.Substring(8, 2) + ":" + endtime.Substring(10, 2) + ":" + endtime.Substring(12, 2));

                            for (int i = 0; i < chcount; i++)
                            {
                                int stepoffset = 0;
                                cdResult.CellID = recMessage.Substring(75 + i * 20, 20);
                                stepoffset = 75 + chcount * 20;

                                cdResult.DOCVVlotage = recMessage.Substring(stepoffset + i * 7, 7);
                                stepoffset = stepoffset + chcount * 7;
                                cdResult.DCIRDifference = recMessage.Substring(stepoffset + i * 6, 6);
                                stepoffset = stepoffset + chcount * 6;
                                cdResult.Voltage = recMessage.Substring(stepoffset + i * 7, 7);
                                stepoffset = stepoffset + chcount * 7;
                                cdResult.Current = recMessage.Substring(stepoffset + i * 7, 7);
                                stepoffset = stepoffset + chcount * 7;
                                
                                cdResult.DCIR = recMessage.Substring(stepoffset + i * 6, 6);
                                stepoffset = stepoffset + chcount * 6;
                                
                                cdResult.CellStatus = recMessage.Substring(stepoffset + i * 2, 2);
                                stepoffset = stepoffset + chcount * 2;
                                cdResult.ChargeVoltage = recMessage.Substring(stepoffset + i * 7, 7);
                                stepoffset = stepoffset + chcount * 7;
                                cdResult.ChargeCurrent = recMessage.Substring(stepoffset + i * 7, 7);
                                stepoffset = stepoffset + chcount * 7;

                                cdResult.CellCH = (i+1).ToString();
                                try
                                {
                                    //mdal.InsertStepResult(cdResult, DCIREndTime);//解析结果写入
                                }
                                catch (Exception ex)
                                {
                                    Exceptionlog.InsertException("存储充放电发送的结果数据时发生异常！", ex.ToString());
                                }
                            }

                            string sendData = Encoding.ASCII.GetString(stx) + "WDD" + PCID + cdResult.StageLoc
                            + "001" + "1" + Encoding.ASCII.GetString(etx);
                            bool flag=iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));

                            while (flag != true && ngcount <= 3)
                            {
                                flag = iSocket.SocketSend(Encoding.ASCII.GetBytes(sendData));
                                ngcount++;
                            }
                            if (flag == false)
                            {
                                //Log.WriteLog(LogFile.SystemError, "通知ECS WRD失败，可能是网络连接导致的");
                                Log.SystemError.Add("通知ECS WRD失败，可能是网络连接导致的");
                            }
                            //Log.WriteLog(LogFile.SendToECS, sendData);//发送记录
                            Log.SendToECS.Add(sendData);
                    }

                    }
                    catch (Exception ee)
                    {
                        Exceptionlog.InsertException("解析充放电发送的结果数据时发生异常！", "解析数据内容：" + recMessage + "\n" + ee.ToString());
                        
                    }
                    //}

                    reciveList.RemoveAt(0);
                   
                //}
                
            }
            return true;
        }
    }
}
